import {useCallback, } from "react";
import {commitLocalUpdate, } from "react-relay";
import {ConnectionHandler, } from "relay-runtime";
import {useRelayEnvironment} from "react-relay/hooks";
import { RecordSourceProxy} from "relay-runtime/lib/store/RelayStoreTypes";
const graphql = require("babel-plugin-relay/macro");


/**不用理会服务器：客户机浏览器界面上直接处理Relay Store，不等后端应答的模式。
 * fragment UserTaskList on Query
 *            @refetchable(queryName: "getTodosQuery") {
 *                 getTodos(where: $iwhere,after:$after,first:$first,orderBy:$orderBy,asc:$asc)
 *                 @connection(key: "Query__getTodos") {
 *                 __id,
 * */
export function useZeebeRead() {
  // const [called, setCalled] =useState<boolean>(false);
  const environment = useRelayEnvironment();
  return {
    call:useCallback(
        (connectionID: string, userTaskId: string) => {
          //const fromObj = store.getRoot();   //正常的 store.get(parentId);
          // const relayRecordSource=environment.getStore().getSource() as RecordSource;
          commitLocalUpdate(environment, storeMe => {
            actAsUpdater(storeMe, connectionID, userTaskId);
          });
          // setCalled(true);
        },
        [environment]
    ),
  };
}

function actAsUpdater(
    store: RecordSourceProxy,
    parentId: string,   //=connectionID : 正常的上一级节点
    deletedID: string,
) {
   const fromObj =store.get(parentId);
  //不能嵌套更新relay store;
   if(!fromObj)   return;
  //这种方式没法针对Query{接口查询List connection}做？  过滤参数还会有影响啊！查询()变量带上; 很多分叉的存储.
  //const connectionID = ConnectionHandler.getConnectionID(deletedID,'Query__getTodos', null);
   ConnectionHandler.deleteNode(fromObj, deletedID);
}


